var MawNumberInputMethod = function(){
	var inited = false;
	var items = ['1','2','3','4','5','6','7','8','9','back','0','close'];
	var inputText = '';
	var inputField = null;
	var container = null;
	var _callback = null; 	
	
	this.create = function(){
		if(inited)
			return;
	 
		var body = document.getElementsByTagName('body')[0];
		
		container = document.createElement('div');
		container.style.zIndex = 30000;
		container.style.position = "absolute";
		container.style.bottom = "0px";
		container.style.width = "100%";
		container.style.display = "none";
    container.id = 'numberInputMethodContainer';
		
		var html="<div style='width:100%;clear:both;'>";
		html +="<input type='text' value='' readonly id='_numberInput_' class='inp_keyboard'/><span id='tips'></span><span class='confirm'>确定</span>";
		html +="</div>";
		html +="<div  class='input_item_container'>";
		items.forEach(function(item){
			var cn = '';
			if(item == 'back'){
				item = " <span style='display:none'>back</span>";
				cn = "clear";
			}else if(item == 'close'){
				item = "关闭";
				cn = "close";	
			}
			html +="<div "+(cn?(" class="+cn+" "):"")+" >"+item+"</div>";
		});
		
		html +="</div>";
		container.innerHTML = html;
		document.body.appendChild(container);
		
		var itemDoms = container.querySelectorAll('.input_item_container > div');
		var len = itemDoms.length;
		inputField = container.querySelector("#_numberInput_"); 
 
		var itemTouchstartLst = function(e){
			var target = e.target;
			var className = target.className || '';
			target.className = className+" item_focus";
			inputText = inputText + target.innerText;
			inputField.value =  inputText;	
			e.preventDefault();
      e.stopPropagation();
		}
		var itemTouchendLst = function(e){
			var target = e.target;
			var className = target.className || '';
			target.className = className.replace(" item_focus",'');;	
		}
		var confirmBtn = container.querySelector('#numberInputMethodContainer .confirm');
		confirmBtn.addEventListener("touchstart",
			function(e){
				if(inputText.length !=0){
					if(_callback){
						
						_callback(inputText);							
					}		
				}else{
					alert("请输入页码");	
				}
				e.preventDefault();
        e.stopPropagation();
			},false);
			
		for(var i=0; i<len; i++){
			var itemDom = itemDoms[i];
			 
			if(itemDom.innerText == ' back'){
				itemDom.addEventListener("touchstart",
					function(e){
						var txt = inputField.value;
						txt = txt.substring(0,txt.length-1);
						inputField.value = txt;
						inputText = txt;	
						e.preventDefault();
            e.stopPropagation();
            
					},false);	
					itemDom.addEventListener("touchend",itemTouchendLst,false);	
			}else if(itemDom.innerText == '关闭'){
				itemDom.addEventListener("touchstart",
					function(e){
						container.style.display = "none";
						e.preventDefault();
            e.stopPropagation();
					},false);	
					itemDom.addEventListener("touchend",itemTouchendLst,false);	
			}else {
				itemDom.addEventListener("touchstart",itemTouchstartLst,false);	
				itemDom.addEventListener("touchend",itemTouchendLst,false);	
				
			}
				
		}
		inited = true;	
	}
 
	var toggle = function(){
		if(container.style.display=='none'){
			container.style.display = '';	
		}else{
			container.style.display ='none';	
		}		
	}
	this.hide = function(){
		container.style.display ='none';
	}
	this.popup = function(obj,tips,callback){
 
		toggle();
		if(tips){
			var _tips = container.querySelector("#tips");
			_tips.innerHTML = tips;
		}
		var val = '';
		if(typeof obj == 'string'){
			val = obj;
		}else{
			var target = obj.target;	
			if(target instanceof HTMLInputElement){
				val = target.value;	
			}else{
				val = target.innerText;
				val = val.split("/")[0].trim();		
			}
		}
		/** 
		inputField.value = val;
 		inputText = val;	
		**/
		inputField.value = '';
 		inputText = '';	
		if(callback){
			_callback = callback;	
		}
		 
	}
}
MawNumberInputMethod = new MawNumberInputMethod();






/**
(function($) {
	$.fn. = function(){
			
	}
})(jq);
**/